import java.util.Iterator;

public class GameBunco implements GameStrategy
{
    private static final int NB_PLAYER = 4;
    private static final int NB_DICE = 3;
    private static final int NB_FACE = 6;
    private static final int NB_ROUND = 6;
    
    private PlayerList playerList;
    private DiceList diceList;
    private int round = 0;
    
    public GameBunco()
    {
        Player player = null;
        
        playerList = new PlayerList();
        for(int i = 0; i < NB_PLAYER; i++)
            playerList.addPlayer(new Player());
        
        diceList = new DiceList();
        for(int i = 0; i < NB_DICE; i++)
            diceList.addDice(new Dice(NB_FACE));

        Iterator<Player> playerIterator = playerList.createIterator();
        
        /* Joue tous les tours */
        for (;this.round < NB_ROUND; this.round++) {
            while (playerIterator.hasNext()) {
                player = playerIterator.next();
                calculateTurnScore(player);
            }
        }
        
        calculateWinner();
    }

    /**
     * Calcul le score de chaque joueur.
     */
    @Override
    public void calculateTurnScore(Player player)
    {
        int[] r = null;
        boolean allSame = true;
        boolean flag = false;
        int first = 0;

        while (true) {
            r = diceList.rollDices();
            first = r[0];
            flag = false;
            
            /* Est-ce que les dés sont tous pareille? */
            for(int i = 1; i < NB_DICE && allSame; i++)
                if (r[i] != first) 
                    allSame = false;
            
            if (allSame) {
                if (first == this.round + 1) {
                    player.addScore(21);
                    break;
                } else {
                    player.addScore(5);
                    continue;
                }
            }
            
            /* Calcul des points seul */
            for (int i: r) {
                if (i == this.round + 1) {
                    player.addScore(1);
                    flag = true;
                }
            }
            
            if (!flag)
                break;
        }
    }

    /**
     * Affiche le ou les gagnants
     */
    @Override
    public void calculateWinner()
    {
        Player player = null;
        Iterator<Player> playerIterator = playerList.createIterator();
        int maxScore = 0;
        int n = 0;
        
        /* Calcul du score maximal */
        while (playerIterator.hasNext()) {
            player = playerIterator.next();
            if (player.getScore() > maxScore) {
                maxScore = player.getScore();
            }
        }

        playerIterator = playerList.createIterator();
        
        /* Imprime tout les joueurs gagnants */
        System.out.print("Winner : Player ");
        
        while (playerIterator.hasNext()) {
            player = playerIterator.next();
            n++;
            if (player.getScore() == maxScore) {
                System.out.print(n + " ");
            }
        }
        
        System.out.println("\n Score : " + maxScore);
    }
}
